﻿using DSLPedItemWizard.Utils;
using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;

namespace DSLPedItemWizard
{
    /// <summary>
    /// Wizard for the creation of a new DSL file
    /// </summary>
    public class NewFileWizard : IWizard
    {
        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }

        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
        }

        public void RunFinished()
        {
        }

        /// <summary>
        /// The wizard is started, opens the wizard button, selects the file type and finishes.
        /// Throws an exception when something goes wrong, it forwards the wizrad to the one
        /// that started it.
        /// </summary>
        public void RunStarted(object automationObject, System.Collections.Generic.Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            var componentModel = VsUtils.GetComponentModel();
            var mainWizardWindow = new MainWizardWindow(componentModel.DefaultCatalog);
            mainWizardWindow.ShowDialog();

            var result = mainWizardWindow.DialogResult;
            if (result == true)
            {
                var fileContent = mainWizardWindow.GetFileContent();
                replacementsDictionary.Add("$dslcontent$", fileContent);
                
            }
            else
            {
                throw new WizardCancelledException();
            }
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}
